package com.yyy.test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class LoginFrame extends JFrame {
    private JTextField usernameField;
    private JPasswordField passwordField;
    private Color backgroundColor = new Color(40, 44, 52);
    private Color buttonColor = new Color(152, 195, 121);
    private Color textColor = new Color(229, 192, 123);

    public LoginFrame() {
        setTitle("Snake Game Login");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400, 300);
        setLocationRelativeTo(null);
        setResizable(false);

        // 创建主面板
        JPanel mainPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                // 设置渐变背景
                Graphics2D g2d = (Graphics2D) g;
                g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                int w = getWidth();
                int h = getHeight();
                GradientPaint gp = new GradientPaint(0, 0, backgroundColor, w, h, new Color(30, 33, 39));
                g2d.setPaint(gp);
                g2d.fillRect(0, 0, w, h);
            }
        };
        mainPanel.setLayout(null);

        // 标题标签
        JLabel titleLabel = new JLabel("Snake Game Login");
        titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
        titleLabel.setForeground(textColor);
        titleLabel.setBounds(100, 30, 200, 40);
        mainPanel.add(titleLabel);

        // 用户名标签和文本框
        JLabel usernameLabel = new JLabel("Username:");
        usernameLabel.setForeground(textColor);
        usernameLabel.setBounds(50, 90, 80, 25);
        mainPanel.add(usernameLabel);

        usernameField = new JTextField();
        usernameField.setBounds(140, 90, 200, 25);
        customizeTextField(usernameField);
        mainPanel.add(usernameField);

        // 密码标签和密码框
        JLabel passwordLabel = new JLabel("Password:");
        passwordLabel.setForeground(textColor);
        passwordLabel.setBounds(50, 130, 80, 25);
        mainPanel.add(passwordLabel);

        passwordField = new JPasswordField();
        passwordField.setBounds(140, 130, 200, 25);
        customizeTextField(passwordField);
        mainPanel.add(passwordField);

        // 登录按钮
        JButton loginButton = new JButton("Login");
        loginButton.setBounds(140, 180, 200, 30);
        customizeButton(loginButton);
        loginButton.addActionListener(e -> validateLogin());
        mainPanel.add(loginButton);

        // 添加键盘事件监听
        addKeyListener(passwordField, loginButton);

        add(mainPanel);
        setVisible(true);
    }

    private void customizeTextField(JTextField textField) {
        textField.setBackground(new Color(50, 54, 62));
        textField.setForeground(Color.WHITE);
        textField.setCaretColor(Color.WHITE);
        textField.setBorder(BorderFactory.createLineBorder(new Color(70, 74, 82)));
        textField.setFont(new Font("Arial", Font.PLAIN, 14));
    }

    private void customizeButton(JButton button) {
        button.setBackground(buttonColor);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorder(BorderFactory.createEmptyBorder(5, 15, 5, 15));
        button.setFont(new Font("Arial", Font.BOLD, 14));

        button.addMouseListener(new MouseAdapter() {
            public void mouseEntered(MouseEvent e) {
                button.setBackground(buttonColor.brighter());
            }

            public void mouseExited(MouseEvent e) {
                button.setBackground(buttonColor);
            }
        });
    }

    private void addKeyListener(JPasswordField passwordField, JButton loginButton) {
        passwordField.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    loginButton.doClick();
                }
            }
        });
    }

    private void validateLogin() {
        String username = usernameField.getText();
        String password = new String(passwordField.getPassword());

        if (username.equals("root") && password.equals("123456")) {
            // 登录成功
            dispose(); // 关闭登录窗口
            SwingUtilities.invokeLater(() -> {
                // 创建游戏窗口
                JFrame frame = new JFrame("Snake Game");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new SnakeGame());
                frame.setResizable(false);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            });
        } else {
            // 登录失败
            JOptionPane.showMessageDialog(this,
                    "Invalid username or password!",
                    "Login Error",
                    JOptionPane.ERROR_MESSAGE);
            passwordField.setText("");
        }
    }
}
